package styledaco.auction;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import statics.SoptVars;
import styledaco.member.MemberProc;

import com.ibatis.sqlmap.client.SqlMapClient;

import comm.util.CommUtil;
import comm.util.RequestManager;
import comm.util.SoptHashMap;
import comm.util.SqlMapLoader;

public class AuctionProc {
	private SqlMapClient sqlMapper = SqlMapLoader.getInstance();
	private Logger logger = Logger.getLogger(MemberProc.class);
	
	public String regGoods(HttpServletRequest request) throws UnsupportedEncodingException{
		String result = "";
		
		SoptHashMap hm = RequestManager.getRequest(request);
		try {
			result = CommUtil.NVL(sqlMapper.insert("auction.regGoods", hm) , "");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String regTender(String memberSeq,String goodsSeq,String tenderCost){
		String result = SoptVars.FAIL;
		SoptHashMap hm = new SoptHashMap();
		hm.put("GOODS_SEQ", goodsSeq);
		hm.put("MEMBER_SEQ", memberSeq);
		hm.put("TENDER_COST", tenderCost);
		try {
			result = sqlMapper.insert("auction.regTender", hm).toString();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	public SoptHashMap getGoodsBySeq(String goodsSeq){
		SoptHashMap hm = null;
		try {
			hm = (SoptHashMap) sqlMapper.queryForObject("auction.getGoodsBySeq",goodsSeq);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hm;
	}
	
	public SoptHashMap getGoodsByBoardSeq(String boardSeq){
		SoptHashMap hm = null;
		try {
			hm = (SoptHashMap) sqlMapper.queryForObject("auction.getGoodsByBoardSeq",boardSeq);
			if(hm!=null && hm.get("GOODS_SEQ")==null)hm = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hm;
	}
	
	public ArrayList getAvalibleGoodsList(){
		ArrayList lt = null;
		
		try {
			lt = (ArrayList) sqlMapper.queryForList("auction.getAvalibleGoodsList");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lt;
	}
	
	public ArrayList getRegistedGoodsList(){
		ArrayList lt = null;
		
		try {
			lt = (ArrayList) sqlMapper.queryForList("auction.getRegistedGoodsList");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lt;
	}
	
	public ArrayList getAwardedGoodsList(){
		ArrayList lt = null;
		
		try {
			lt = (ArrayList) sqlMapper.queryForList("auction.getAwardedGoodsList");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lt;
	}
	public String updateGoodsAddToBoard(String boardSeq,String goodsSeq){
		String result = "";
		if(boardSeq.length()>0 && goodsSeq.length()>0){
		SoptHashMap hm = new SoptHashMap();
		hm.put("BOARD_SEQ", boardSeq);
		hm.put("GOODS_SEQ", goodsSeq);
		
		try {
			result = String.valueOf(sqlMapper.update("auction.updateGoodsAddToBoard", hm));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		
		return result;
	}
}
